"""
Description: 岗位视图
Author: 月间
Date: 2025-05-19 23:53:48
LastEditTime: 2025-10-22 22:03:29
LastEditors:
"""

# @Version        : 1.0
# @Update Time    : 2024/12/11 22:23
# @File           : post_controller.py
# @IDE            : PyCharm
# @Desc           : 文件描述信息
from fastapi import APIRouter, Depends
from fastapi_utils.cbv import cbv
from sqlalchemy import and_
from sqlalchemy.ext.asyncio import AsyncSession

from authx.dependencies.permission import PermissionChecker
from src import schemas
from src.core.base_controller import BaseController
from src.core.dependencies import IdList
from src.core.response.response_schema import (
    PageResponseDataSchema,
    ResponseGenericSchema,
    ResponseSchema,
    PageResponse,
)
from src.db.db_database import alchemy
from src.models import SysPost
from src.schemas.post_schema import PostOptions, PostParams
from src.services.post_service import PostService

router = APIRouter()


@cbv(router)
class PostController(BaseController):
    db: AsyncSession = Depends(alchemy.provide_session())
    post_service: PostService = Depends(alchemy.provide_service(PostService))

    @router.get(
        "/list",
        summary="岗位列表",
        response_model=PageResponse[schemas.PostOut],
        dependencies=[Depends(PermissionChecker("system:post:list"))],
    )
    async def get_post_list(self, param: PostParams = Depends()):
        where = await param.get_where_and_limit_offset(self.db)
        res, count = await self.post_service.list_and_count(*where)
        return self.paginated_response(
            data=res, total=count, current=param.page, size=param.limit
        )

    @router.get(
        "/all",
        summary="获取所有岗位",
        response_model=ResponseGenericSchema[list[PostOptions]],
        dependencies=[Depends(PermissionChecker("system:post:list"))],
    )
    async def get_all_post(self):
        res = await self.post_service.list(and_(SysPost.status == "1"))
        return self.success(data=[{"label": i.post_name, "value": i.id} for i in res])

    @router.post(
        "/add",
        summary="创建岗位",
        dependencies=[Depends(PermissionChecker("system:post:add"))],
        response_model=ResponseSchema,
    )
    async def add_post(
        self,
        model: schemas.PostCreate,
    ):
        await self.post_service.create(model, auto_commit=True)
        return self.success(message="创建成功")

    @router.put(
        "/update",
        summary="更新岗位",
        dependencies=[Depends(PermissionChecker("system:post:update"))],
        response_model=ResponseSchema,
    )
    async def update_post(
        self,
        model: schemas.PostUpdate,
    ):
        await self.post_service.update(model, auto_commit=True)
        return self.success(message="更新成功")

    @router.delete(
        "/delete",
        summary="删除岗位",
        dependencies=[Depends(PermissionChecker("system:post:delete"))],
        response_model=ResponseSchema,
    )
    async def delete_post(
        self,
        ids: IdList = Depends(),
    ):
        await self.post_service.delete_many(ids.ids, auto_commit=True)
        return self.success(message="删除成功")
